package se.webbzon.oschi01.world;

import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DefaultRegion implements Region {
	
	private final Point coordinate;
	
	private List<Chunk> chunks;
	
	public DefaultRegion() {
		this(0,0);
	}
	
	public DefaultRegion(int x, int z) {
		chunks = new ArrayList<Chunk>();
		coordinate = new Point(x,z);
	}
	
	@Override public Point2D getCoordinate() {
		return coordinate;
	}
	
	@Override public void setCoordinate(int x, int z) {
		coordinate.setLocation(x, z);
	}
	
	@Override public boolean addChunk(Chunk chunk) {
		return chunks.add(chunk);
	}
	
	@Override public boolean removeChunk(Chunk chunk) {
		return chunks.remove(chunk);
	}
	
	@Override public Chunk getChunk(Point2D coordinates) {
		for (Chunk chunk : chunks) {
			if (chunk.getCoordinate().equals(coordinates))
				return chunk;
		}
		return null;
	}
	
	@Override public int chunks() {
		return chunks.size();
	}

	@Override public Chunk getChunk(int i) {
		return chunks.get(i);
	}

	@Override public boolean hasChunk(Chunk chunk) {
		return chunks.contains(chunk);
	}
	
	@Override public String toString() {
		Point2D coord = getCoordinate();
		return "[x=" + coord.getX() + ",z="+ coord.getY() + "]";
	}

	@Override public boolean writeback() throws IOException {
		return false;
	}

	@Override public void free() {
		for (Chunk chunk : chunks) {
			chunk.free();
		}
	}

}
